#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <unistd.h>
#include <signal.h>

void sig_quit(int signo) {
	printf("caught SIGQUIT\n");
	if (signal(SIGQUIT, SIG_DFL)==SIG_ERR) {
		printf("can't reset SIGQUIT\n");
		return;
	}
}

int main() {
	sigset_t newmask, oldmask, pendmask;
	if (signal(SIGQUIT, sig_quit) == SIG_ERR) {
		printf("signal quit error\n");
		return -1;
	}

	sigemptyset(&newmask);
	sigaddset(&newmask, SIGQUIT);
	if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
		printf("block quit error\n");
		return -1;
	}
	printf("block SIGQUIT\n");

	sleep(5);

	if (sigpending(&pendmask) < 0) {
		printf("pending quit error\n");
		return -1;
	}
	if (sigismember(&pendmask, SIGQUIT)) 
		printf("SIGQUIT pending\n");

	if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
		printf("unblock quit error\n");
		return -1;
	}
	printf("unblock SIGQUIT\n");

	sleep(5);

	return 0;
}